﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;

namespace Cashier_DAO.db
{
    public class ConnectionFactory
    {
        public static DatabaseType DatabaseType;

        /// <summary>
        /// 静态构造函数
        /// </summary>
        static ConnectionFactory()
        {
            if (String.IsNullOrEmpty(DbConfig.Dbtype))
                DatabaseType = DatabaseType.SQLite;
            else if (DbConfig.Dbtype.ToLower().Equals("sqlite"))
                DatabaseType = DatabaseType.SQLite;
            else if (DbConfig.Dbtype.ToLower().Equals("mysql"))
                DatabaseType = DatabaseType.MySQL;
            else if (DbConfig.Dbtype.ToLower().Equals("sqlserver"))
                DatabaseType = DatabaseType.SQLServer;
            else if (DbConfig.Dbtype.ToLower().Equals("oracle"))
                DatabaseType = DatabaseType.Oracle;
            else
                DatabaseType = DatabaseType.SQLite;
        }

        /// <summary>
        /// 返回DbConnenct实例
        /// </summary>
        /// <returns></returns>
        public static DbConnection GetDbConnection()
        {
            DbConnection conn = null;
            try
            {
                if (DatabaseType == DatabaseType.SQLite)
                    conn = SQLiteConnectionInstance.GetSQLiteConnection();
                else if (DatabaseType == DatabaseType.MySQL)
                    conn = MySQLConnectionInstance.GetMySqlConnection();
                else if (DatabaseType == DatabaseType.SQLServer)
                    conn = SQLConnectionInstance.GetSqlConnection();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return conn;
        }
    }
}
